package com.yds.dccjll.androidhome.base.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yds.dccjll.androidhome.ListActivity;
import com.yds.dccjll.androidhome.R;
import com.yds.dccjll.androidhome.base.entry.EndItem;
import com.yds.dccjll.androidhome.base.entry.MiddleItem;
import com.yds.dccjll.lib.base.adapter.SimpleExternalRVAdapter;
import com.yds.dccjll.lib.base.util.DensityUtil;
import com.yds.dccjll.lib.base.util.LogUtil;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dccjll on 2017/5/28.
 * App相关的工具
 */

public class AppUtil {

    private static final String TAG = "AppUtil";

    public static List<Object> initItemList(Context context, int arrayStringResId) {
        if (context == null) {
            throw new IllegalArgumentException("not initial context");
        }
        List<Object> itemList = new ArrayList<>();
        String[] mainTypeArr = context.getResources().getStringArray(arrayStringResId);
        for (int i = 0; i < mainTypeArr.length; i++) {
            if (mainTypeArr[i].contains("*") && mainTypeArr[i].split("\\*").length == 2) {//是过渡条目
                MiddleItem middleItem = new MiddleItem();
                middleItem.setItemName(mainTypeArr[i].split("\\*")[0]);
                List<EndItem> endItemList = new ArrayList<>();
                String subItemListName = mainTypeArr[i].split("\\*")[1];
                try {
                    Field subItemListNameResIdField = R.array.class.getDeclaredField(subItemListName);
                    int subItemListNameResId = (int) subItemListNameResIdField.get(R.array.class);
                    String[] subItemList = context.getResources().getStringArray(subItemListNameResId);
                    for (String subItemString : subItemList) {
                        if (subItemString.contains("#") && subItemString.split("#").length == 3) {
                            EndItem endItem = new EndItem();
                            endItem.setItemName(subItemString.split("#")[0]);
                            try {
                                endItem.setNoteClazz(Class.forName(subItemString.split("#")[1]));
                                endItem.setDemoClazz(Class.forName(subItemString.split("#")[2]));
                            } catch (ClassNotFoundException e) {
                                e.printStackTrace();
                            }
                            endItemList.add(endItem);
                        } else {
                            LogUtil.e(TAG, "无法识别的类型定义,subItemString=" + subItemString);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                middleItem.setEndItemList(endItemList);
                itemList.add(middleItem);
            }else if (mainTypeArr[i].contains("#") && mainTypeArr[i].split("#").length == 3) {
                EndItem endItem = new EndItem();
                endItem.setItemName(mainTypeArr[i].split("#")[0]);
                try {
                    endItem.setNoteClazz(Class.forName(mainTypeArr[i].split("#")[1]));
                    endItem.setDemoClazz(Class.forName(mainTypeArr[i].split("#")[2]));
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
                itemList.add(endItem);
            } else {
                LogUtil.e(TAG, "无法识别的类型定义,mainTypeArr[" + i + "]=" + mainTypeArr[i]);
            }
        }
        return itemList;
    }

    public static void initAdapter(final Activity activity, List<Object> itemList) {
        RecyclerView mainRecyclerView = (RecyclerView) activity.findViewById(R.id.mainRecyclerView);
        mainRecyclerView.setLayoutManager(new LinearLayoutManager(activity));
        SimpleExternalRVAdapter<MiddleItem, EndItem> simpleExternalRVAdapter = new SimpleExternalRVAdapter<MiddleItem, EndItem>(activity, itemList, R.layout.item_common_1, R.layout.item_common_2) {
            @Override
            public void onBindAssignedNullViewHolder(RecyclerView.Adapter adapter, RVViewHolder rvViewHolde) {

            }

            @Override
            public void onBindDefaultNullViewHolder(RecyclerView.Adapter adapter, TextView defaultNullTextView) {

            }

            @Override
            public int getItemViewTypeByPosition(int position) {
                if (getItem(position) instanceof MiddleItem) {
                    return VIEW_TYPE_T;
                }  else {
                    return VIEW_TYPE_M;
                }
            }

            @Override
            public void onBindTViewHolder(RecyclerView.Adapter adapter, RVViewHolder rvViewHolder, int position, final MiddleItem entry, List<MiddleItem> data) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                if (position != 0) {
                    layoutParams.setMargins(0, DensityUtil.dp2px(activity, 10), 0, 0);
                }
                rvViewHolder.findViewById(R.id.common_1_layout).setLayoutParams(layoutParams);
                ((Button)rvViewHolder.findViewById(R.id.itemButton)).setText(entry.getItemName());
                rvViewHolder.findViewById(R.id.itemButton).setOnClickListener(
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(activity, ListActivity.class);
                                intent.putExtra("titleInfo", entry.getItemName());
                                intent.putExtra("itemList", (Serializable) entry.getEndItemList());
                                activity.startActivity(intent);
                            }
                        }
                );
            }

            @Override
            public void onBindMViewHolder(RecyclerView.Adapter adapter, RVViewHolder rvViewHolder, int position, final EndItem entry, List<EndItem> data) {
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                if (position != 0) {
                    layoutParams.setMargins(0, DensityUtil.dp2px(activity, 10), 0, 0);
                }
                rvViewHolder.findViewById(R.id.common_2_layout).setLayoutParams(layoutParams);
                ((Button)rvViewHolder.findViewById(R.id.itemButton)).setText(entry.getItemName());
                try {
                    ((Button)rvViewHolder.findViewById(R.id.demoButton)).setText(entry.getNoteClazz().getSimpleName());
                    rvViewHolder.findViewById(R.id.demoButton).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            try {
                                Intent intent = new Intent(activity, Class.forName(entry.getDemoClazz().getName()));
                                intent.putExtra("titleInfo", entry.getItemName());
                                activity.startActivity(intent);
                            } catch (ClassNotFoundException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                    ((Button)rvViewHolder.findViewById(R.id.noteButton)).setText(entry.getDemoClazz().getSimpleName());
                    rvViewHolder.findViewById(R.id.noteButton).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            try {
                                Intent intent = new Intent(activity, Class.forName(entry.getNoteClazz().getName()));
                                intent.putExtra("titleInfo", entry.getItemName());
                                activity.startActivity(intent);
                            } catch (ClassNotFoundException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        mainRecyclerView.setAdapter(simpleExternalRVAdapter);
    }
}
